package pl.polsl.mmm.projectcomm.objects;

import android.content.Context;

import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.dynamics.World;

import pl.polsl.mmm.projectcomm.Constants;
import pl.polsl.mmm.projectcomm.R;
import pl.polsl.mmm.projectcomm.data.VertexArray;
import pl.polsl.mmm.projectcomm.programs.BackgroundShaderProgram;
import pl.polsl.mmm.projectcomm.util.BackgroundTextureHelper;

import static android.opengl.GLES20.GL_TRIANGLES;
import static android.opengl.GLES20.glDrawArrays;

/**
 * Created by michal on 08.02.15.
 */
public class Platform {
    private static final int POSITION_COMPONENT_COUNT = 3;
    private static final int TEXTURE_COORDINATES_COMPONENT_COUNT = 2;
    private static final int STRIDE = (POSITION_COMPONENT_COUNT
            + TEXTURE_COORDINATES_COMPONENT_COUNT) * Constants.BYTES_PER_FLOAT;

    //id tekstury w api OpenGL
    private int texture;
    private World world;
    private boolean hidden = false;

    private VertexArray vertexArray;

    private void setDimensions (float x, float y, float width, float height) {

        float halfHeight = height / 2;
        float halfWidth = width / 2;

        float[] newVertexPositionArray = {
                x-halfWidth, y-halfHeight,  0f,     0f , 0f, //1
                x-halfWidth, y+halfHeight,  0f,     0f , 1f, //2
                x+halfWidth, y+halfHeight,  0f,     1f , 1f, //3

                x-halfWidth, y-halfHeight,  0f,     0f , 0f, //1
                x+halfWidth, y+halfHeight,  0f,     1f , 1f, //3
                x+halfWidth, y-halfHeight,  0f,     1f , 0f, //4
        };

        vertexArray = new VertexArray(newVertexPositionArray);
    }

    public Platform(Context context, World world, float x, float y, float width, float height, boolean hidden) {
        texture = BackgroundTextureHelper.loadTexture(context, R.drawable.platform2);
        this.hidden = hidden;
        // y/2
        setDimensions(x, y/2, width, height*2);

        // Fizyka
        this.world = world;

        BodyDef platformDef = new BodyDef();
        platformDef.position.set(2*x, y/2-3f);
        platformDef.type = BodyType.STATIC;
        PolygonShape platformShape = new PolygonShape();
        platformShape.setAsBox(width, height / 4.0f);
        Body platform = world.createBody(platformDef);
        FixtureDef platformFixture = new FixtureDef();
        platformFixture.density = 1;
        platformFixture.friction = 1f;
        platformFixture.shape = platformShape;
        platform.createFixture(platformFixture);
    }

    public void bindData(BackgroundShaderProgram textureProgram) {
        textureProgram.setTexture(texture);
        vertexArray.setVertexAttribPointer(
                0,
                textureProgram.getPositionAttributeLocation(),
                POSITION_COMPONENT_COUNT,
                STRIDE);
        vertexArray.setVertexAttribPointer(
                POSITION_COMPONENT_COUNT,
                textureProgram.getTextureCoordinatesAttributeLocation(),
                TEXTURE_COORDINATES_COMPONENT_COUNT,
                STRIDE);
    }

    public void draw() {
        glDrawArrays(GL_TRIANGLES,0,6);
    }

    public void setHidden(boolean hidden) {
        this.hidden = hidden;
    }

    public boolean isHidden() {
        return this.hidden;
    }
}
